package com.wanjia.estate.service.bdservice.bdcdjxxcxjgzmservice;

import com.wanjia.estate.controller.OtherController;
import com.wanjia.estate.mapper.YtMapper;
import com.wanjia.estate.pojo.entity.Bdcdjxxjgcx;
import com.wanjia.estate.pojo.entity.Details;
import com.wanjia.estate.pojo.entity.HomeInfoDetail;
import com.wanjia.estate.pojo.entity.TaoCi;
import com.wanjia.estate.pojo.model.JsonResult;
import com.wanjia.estate.properties.FileTemplateProperties;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExcelReaderWriterBdcService {

    @Autowired
    private FileTemplateProperties fileTemplateProperties;

    @Autowired
    YtMapper mapper;

    @Autowired
    EnquiryOfMinorsService enquiryOfMinorsService;

    @Autowired
    OtherController otherController;

    public String ExcelReaderWriter(List<Bdcdjxxjgcx> qlrlist, List<Bdcdjxxjgcx> xcrlist, TaoCi taoCi,Bdcdjxxjgcx bdcdjxxjgcx, String folder) {
        try {
            FileInputStream inputStream = new FileInputStream(fileTemplateProperties.getBdcdjxxcxjgzm());
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            Row row2 = sheet.getRow(1);
            row2.getCell(3).setCellValue(bdcdjxxjgcx.getYcslywh());
            row2.getCell(7).setCellValue(bdcdjxxjgcx.getYwlx());

            Row row3 = sheet.getRow(2);
            row3.getCell(3).setCellValue(bdcdjxxjgcx.getCxrlx());

            String cxr = qlrlist.stream()
                    .map(obj -> obj.getCxr())
                    .collect(Collectors.joining(", "));

            String xcr = xcrlist.stream()
                    .map(obj -> obj.getXcr())
                    .collect(Collectors.joining(", "));


            //添加查询人

            Row row4 = sheet.getRow(3);
            row4.getCell(3).setCellValue(bdcdjxxjgcx.getCxr1());

//            //添加协查人
//            List<Details> details = taoCi.getDetails();
//            for (Details detail : details) {
//                JsonResult<HomeInfoDetail> marriage = otherController.marriage(detail);
//                // 查询配偶和未成年子女，配偶如果和details重复 就是协查人 其他然后区分
//            }

            //如果查询人是单人，协查人就是配偶+小孩
            //如果查询人是夫妻，协查人就是小孩

            Row row5 = sheet.getRow(4);
            row5.getCell(3).setCellValue(taoCi.getXcr());

            //已有房产（查询人+协查人）
            Row row7 = sheet.getRow(6);
            row7.getCell(1).setCellValue(cxr+","+xcr);

            String xcrCqzh = xcrlist.stream()
                    .map(obj -> obj.getCqzh())
                    .distinct()
                    .collect(Collectors.joining(", "));

            String cxrCqzh = qlrlist.stream()
                    .map(obj -> obj.getCqzh())
                    .distinct()
                    .collect(Collectors.joining(", "));

            row7.getCell(3).setCellValue(cxrCqzh+","+xcrCqzh);
            //登记时间
            row7.getCell(4).setCellValue("");

            String xcrFwyt = xcrlist.stream()
                    .map(obj -> obj.getFwyt())
                    .distinct()
                    .collect(Collectors.joining(", "));

            String cxrFwyt = qlrlist.stream()
                    .map(obj -> obj.getFwyt())
                    .distinct()
                    .collect(Collectors.joining(", "));
            row7.getCell(5).setCellValue(cxrFwyt+","+xcrFwyt);

            String xcrZl = xcrlist.stream()
                    .map(obj -> obj.getZl())
                    .distinct()
                    .collect(Collectors.joining(", "));

            String cxrZl = qlrlist.stream()
                    .map(obj -> obj.getZl())
                    .distinct()
                    .collect(Collectors.joining(", "));

            row7.getCell(6).setCellValue(cxrZl+","+xcrZl);

            String xcrGyfs = xcrlist.stream()
                    .map(obj -> obj.getGyfs())
                    .collect(Collectors.joining(", "));

            String cxrGyfs = qlrlist.stream()
                    .map(obj -> obj.getGyfs())
                    .collect(Collectors.joining(", "));
            row7.getCell(8).setCellValue(cxrGyfs+","+xcrGyfs);

            //现在办理的房产
            Row row11 = sheet.getRow(10);
            row11.getCell(1).setCellValue(bdcdjxxjgcx.getCxr1());


            row11.getCell(3).setCellValue(bdcdjxxjgcx.getCqzh1());
            //登记时间
            row11.getCell(4).setCellValue(bdcdjxxjgcx.getSqsj());

            String fwyt = mapper.getFwyt(bdcdjxxjgcx.getFwyt1());
            row11.getCell(5).setCellValue(fwyt);


            row11.getCell(6).setCellValue(bdcdjxxjgcx.getZl1());


            row11.getCell(8).setCellValue(bdcdjxxjgcx.getGyfs1());


            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formattedDate = formatter.format(date);
            Row row15 = sheet.getRow(14);
            row15.getCell(6).setCellValue(formattedDate);


            FileOutputStream outputStream = new FileOutputStream(folder+"/" + "bdcdjxxjgcx.xlsx");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
        File file = new File(folder+"/"+ "bdcdjxxjgcx.xlsx");
        return file.getPath();
    }
}
